/**
 * Filename: OfficialAlbumListActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 16:20
 */
package com.github.postsanf.yinian.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.fragment.NewStudentAlbumListFragment;
import com.github.postsanf.yinian.fragment.OffciaAlbumListFragment;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.umeng.analytics.MobclickAgent;

/**
 * 忆年官方相册列表
 */
public class OfficialAlbumListActivity extends BaseActivity implements ISkinUpdate, View.OnClickListener {
    private Toolbar mToolbar;
    private OffciaAlbumListFragment offciaAlbumListFragment;
    private NewStudentAlbumListFragment newStudentAlbumListFragment;
    private LinearLayout btn_officalalbum;
    private LinearLayout btn_newstudent;
    private ImageView iv_newstudent;
    private ImageView iv_officalalbum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_official_album);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.album_official_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        btn_officalalbum = (LinearLayout) findViewById(R.id.btn_officalalbum);
        btn_newstudent = (LinearLayout) findViewById(R.id.btn_newstudent);
        iv_officalalbum = (ImageView) findViewById(R.id.iv_officalalbum);
        iv_newstudent = (ImageView) findViewById(R.id.iv_newstudent);
        btn_officalalbum.setBackgroundResource(R.drawable.office_bar_bg_green);
        btn_officalalbum.setOnClickListener(this);
        btn_newstudent.setOnClickListener(this);

        offciaAlbumListFragment = new OffciaAlbumListFragment();
        newStudentAlbumListFragment = new NewStudentAlbumListFragment();
    }

    @Override
    public void onClick(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.btn_officalalbum:
                btn_officalalbum.setBackgroundResource(R.drawable.office_bar_bg_green);
                btn_newstudent.setBackgroundResource(R.drawable.office_bar_bg_white);
                iv_officalalbum.setImageResource(R.drawable.office_bar_black);
                iv_newstudent.setImageResource(R.drawable.newstudent_bar_white);
                transaction.replace(R.id.fragmentpager, offciaAlbumListFragment);
                break;
            case R.id.btn_newstudent:
                btn_newstudent.setBackgroundResource(R.drawable.office_bar_bg_green);
                btn_officalalbum.setBackgroundResource(R.drawable.office_bar_bg_white);
                iv_newstudent.setImageResource(R.drawable.newstudent_bar_black);
                iv_officalalbum.setImageResource(R.drawable.office_bar_white);
                transaction.replace(R.id.fragmentpager, newStudentAlbumListFragment);
                break;
        }
        transaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}

